#pragma once
#include <iostream>
#include <ctemplate/template.h>
// #include "oj_model.hpp"
#include "oj_mysql_model.hpp"
#include "../comm/log.hpp"
#include "../comm/util.hpp"

namespace ns_view
{
    using namespace std;
    using namespace ns_log;
    using namespace ns_util;
    using namespace ns_model;

    const string template_path =  "./template_html/";
    class View
    {
    public:
        View() {}
        ~View() {}
    public:
        void expandAllQuestions(const vector<struct Question>& questions,string* html)
        {
            //形成路径
            string src_html = template_path + "all_questions.html";
            //创建数字字典
            ctemplate::TemplateDictionary root("all_questions");
            for(auto q: questions)
            {
                ctemplate::TemplateDictionary * sub = root.AddSectionDictionary("questions_list");
                sub->SetValue("number",q.number);
                sub->SetValue("title",q.tile);
                sub->SetValue("star",q.star);
            }
            //获取被渲染的html
            ctemplate::Template * tpl = ctemplate::Template::GetTemplate(src_html,ctemplate::DO_NOT_STRIP);
            //进行渲染
            tpl->Expand(html,&root);
        }

        void expandOneQuestion(const struct Question& q,string* html)
        {
             //形成路径
            string src_html = template_path + "one_question.html";
            //创建数字字典
            ctemplate::TemplateDictionary root("one_question");
            root.SetValue("number",q.number);
            root.SetValue("title",q.tile);
            root.SetValue("star",q.star);
            root.SetValue("desc",q.desc);
            root.SetValue("pre_code",q.header);
            //获取被渲染的html
            ctemplate::Template * tpl = ctemplate::Template::GetTemplate(src_html,ctemplate::DO_NOT_STRIP);
            //进行渲染
            tpl->Expand(html,&root);
        }
    };
}